import React from 'react';
import echarts from 'echarts';

import ReactEcharts from 'echarts-for-react';
import { LineProps } from '../chart.interface';

export class LineChart extends React.Component<LineProps, {}> {
  static defaultProps = {
    ySplitLine: true,
    xAxisShow: true,
    height: '240px',
    categories: [],
    series: [],
  };

  constructor(props: LineProps) {
    super(props);
  }

  prepareOption = (): any => {
    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: 'rgba(0, 255, 233,0)',
                },
                {
                  offset: 0.5,
                  color: 'rgba(255, 255, 255,1)',
                },
                {
                  offset: 1,
                  color: 'rgba(0, 255, 233,0)',
                },
              ],
              global: false,
            },
          },
        },
      },
      grid: {
        top: '15%',
        left: '5%',
        right: '5%',
        bottom: '15%',
      },
      xAxis: [
        {
          show: this.props.xAxisShow,
          type: 'category',
          axisLine: {
            show: false,
          },
          splitArea: {
            // show: true,
            color: '#f00',
            lineStyle: {
              color: '#f00',
            },
          },
          axisLabel: {
            color: '#fff',
          },
          boundaryGap: false,
          data: this.props.categories,
        },
      ],
      yAxis: [
        {
          type: 'value',
          min: 0,
          splitNumber: 4,
          splitLine: {
            show: this.props.ySplitLine,
            lineStyle: {
              color: 'rgba(255,255,255,0.1)',
            },
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
            margin: 20,
            textStyle: {
              color: '#d1e6eb',
            },
          },
          axisTick: {
            show: false,
          },
        },
      ],
      series: [] as any,
    };

    for (const s of this.props.series!) {
      option.series.push({
        name: s.name,
        type: 'line',
        smooth: true,
        showAllSymbol: false,
        symbol: 'circle',
        symbolSize: 6,
        lineStyle: {
          normal: {
            color: '#00b3f4',
          },
        },
        itemStyle: {
          color: 'yellow',
        },
        tooltip: {
          show: true,
        },
        areaStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(
              0,
              0,
              0,
              1,
              [
                {
                  offset: 0,
                  color: 'rgba(0,179,244,0.3)',
                },
                {
                  offset: 1,
                  color: 'rgba(0,179,244,0)',
                },
              ],
              false,
            ),
            shadowColor: 'rgba(0,179,244, 0.9)',
            shadowBlur: 20,
          },
        },
        data: s.data,
      });
    }

    return option;
  };

  render() {
    return (
      <ReactEcharts option={...this.prepareOption()} style={{ height: this.props.height, width: '100%' }} />
    );
  }
}
